﻿package controllers.read;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rbac.models.User;
import utils.NumberUtil;
import models.Stzd;
import models.Sysinfo;

import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.JspView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;

import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import controllers.ApplicationController;
import java.text.SimpleDateFormat;

public class SysinfoController extends ApplicationController {
	
	public void index() throws Exception{
		
	}
	
	
	
	public void show(String ids) throws Exception{
		  //参数只能是数字与逗号
		  if (NumberUtil.isNumber(ids)){
		    List<Sysinfo> infos = Sysinfo.findAll(Sysinfo.class,"passflag = 0 and id in ("+ids+")",null,"editdate desc");
		    request.setAttribute("infos", infos);	
		  }
		}
	
	public JspView showNewSysinfos() throws Exception {
		String orderBy = "editdate desc";
		String cond = "passflag = 0 and totop = 1 and totopdate >getdate()";

		
		List<Sysinfo> items = Sysinfo.findAll(Sysinfo.class, cond, null,orderBy);
		
		request.setAttribute("infos", items);	
		JspView view = new JspView("/read/sysinfo/shownews.jsp"); 
		return view;
	}
	
	public View getSysinfoItems(int page, int rows, String q,int t, String sort, String order) throws Exception {
		String orderBy = "editdate desc";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = "passflag = 0 and getdate() <= valid_time";
		List<Object> tmpArgs = new ArrayList<Object>();
		
		/**
		if (t != 0){
			cond += " and (parentId = ?)";
			tmpArgs.add(t);
		}
		**/

		
		if (q != null){
			cond += " and (context like '%"+q+"%' or sysinfo_type like '%"+q+"%' ) ";			
		}
		

		
		Object[] args = tmpArgs.toArray();
		
		long total = Sysinfo.count(Sysinfo.class, cond, args);
		List<Sysinfo> items = Sysinfo.findAll(Sysinfo.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	
}
